package org.proddevplm.business.resource;

import java.util.Date;
import java.util.Map;

import org.proddevplm.business.api.AController;
import org.proddevplm.business.resource.constants.ResourceConstants;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.constants.UserConstants;
import org.proddevplm.dao.data.Resource;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.data.activities.Activity;

public class ResourceController extends AController {

    @Override
    public Object execute(int command, Object data) {
        Map<String, Object> resourceMap = (Map<String, Object>) data;
        int productId = (Integer) getSession().getAttribute("productId");
        User user = (User) getSession().getAttribute(UserConstants.USER);
        
        switch (command) {

            case ResourceConstants.UPDATE_RESOURCE:
                //System.out.println("====== UDATE RESOURCE ==== ");
                String fileName = (String) resourceMap.get("fileName");
                System.out.println("getting from: "+fileName+" "+productId);
                Resource res = getDaoManager().getResourceManager().getResourceByNameandProductId(fileName, productId);

                if (res == null) {
                    System.out.println("NULL OBJECT ##############");
                    res = new Resource();
                    res.setAuthor(user.getSurname() + " " + user.getName());
                    res.setName(fileName);
                    res.setVersion(1);
                } else {

                    res.setVersion(res.getVersion()+1);
                }
                
                if("fromExternalTool".equals(resourceMap.get("uploadType").toString())){
                	res.setActivityName(resourceMap.get("activityName").toString());
                	Activity activity = getDaoManager().getActivityManager().getActivityByNameandProductId(resourceMap.get("activityName").toString(), productId);
                	res.setStatus(activity.getActivityLifeCycle());
                	res.setDevPhase("Concept Development");//de modificat
                }else if("fromResources".equals(resourceMap.get("uploadType").toString())){
                	res.setStatus((String) resourceMap.get("fileStatus"));
                    res.setDevPhase((String) resourceMap.get("fileDevStage"));
                }else if("fromExternalToolApprover".equals(resourceMap.get("uploadType").toString())){
                	res.setStatus("Approved");
                }
                
                res.setDetails((String) resourceMap.get("fileComments"));
                res.setDate(new Date().toString());
                res.setModifier(user.getSurname() + " " + user.getName());
                res.setLocked(false);
                res.setProductId(productId);
                String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                res.setType(fileType);

                System.out.println(res.toString());
                getDaoManager().getResourceManager().updateResource(res);

                System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                System.out.println(res.toString());
                System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                
                if("fromExternalTool".equals(resourceMap.get("uploadType").toString())){
                	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
                	putOnSession("currentTab", "tab4");
                }else if("fromExternalToolApprover".equals(resourceMap.get("uploadType").toString())){
                	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
                	putOnSession("currentTab", "tab4");
                }else{
                	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
                	putOnSession("currentTab", "tab6");
                }
                break;

            case ResourceConstants.DELETE_RESOURCE:
                String resName = (String) resourceMap.get("resName");
                getDaoManager().getResourceManager().removeResource(resName, productId);
                break;
        }

        return "response";
    }

}
